//
//  RecommendViewController.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/7.
//

#import "RecommendViewController.h"
#import "../View/GTNormalTableViewCell.h"
#import "../View/GTDeleteCellView.h"
#import "../Model/GTListLoader.h"
//#import "GTDetailViewController.h"
#import "../../Utils/GTMediator.h"
#import "../Model/GTListItem.h"
#import "../View/GTSearchBar.h"
#import "../../Utils/GTScreen.h"
@interface RecommendViewController ()<UITableViewDataSource,UITableViewDelegate, GTNormalTableViewCellDelegate>
@property(nonatomic, strong, readwrite)UITableView *tableView;
@property(nonatomic, strong, readwrite)NSArray *dataArray;
@property(nonatomic, strong, readwrite)GTListLoader * listLoader;
@end

@implementation RecommendViewController

- (instancetype) init{
	self = [super init];
	if(self){
//		self.dataArray = @[].mutableCopy;
//		for (int i=0; i<20; i++) {
//			[self.dataArray addObject:@(i)];
//		}
		self.tabBarItem.title=@"推荐";
		self.tabBarItem.image = [UIImage imageNamed:@"like"];
		self.tabBarItem.selectedImage = [UIImage imageNamed:@"like_selected"];
	}
	return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	self.view.backgroundColor = [UIColor whiteColor];
	self.navigationController.navigationBar.barTintColor = [UIColor redColor];
	[self.tabBarController.navigationItem setTitleView:({
		GTSearchBar *searchBar = [[GTSearchBar alloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.height-UI(20)*2 , self.navigationController.navigationBar.bounds.size.height)];
		searchBar;
	})];
	self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
	
	self.tableView.dataSource = self;
	self.tableView.delegate = self;
	[self.view addSubview:self.tableView];
	self.listLoader = [[GTListLoader alloc]init];
	__weak typeof(self)wself = self;
	[self.listLoader loadListDataWithFinishBlck:^(BOOL success, NSArray<GTListItem *> *dataArray) {
		__strong typeof(wself) strongSelf = wself;
		if(success){
			strongSelf.dataArray = dataArray;
			[strongSelf.tableView reloadData];
		}
	}];
}

- (void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
	
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	GTNormalTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"id"];
	if(!tableViewCell){
		tableViewCell = [[GTNormalTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"id"];
		tableViewCell.delegate = self;
	}
	[tableViewCell layoutTableViewCellWithItem:[self.dataArray objectAtIndex:indexPath.row]];
	
//	tableViewCell.textLabel.text = [NSString stringWithFormat:@"主标题 - %lu", indexPath.row];
//	tableViewCell.detailTextLabel.text = @"副标题";
//	tableViewCell.imageView.image = [UIImage imageNamed:@"video"];
	return tableViewCell;
}

#pragma mark - GTNormalTableViewCellDelegate
-(void) tableViewCell:(UITableViewCell *)tableViewCell clickDeleteButton:(UIButton *)deleteButton{
	NSLog(@"");
//	GTDeleteCellView *deleteView = [[GTDeleteCellView alloc]initWithFrame:self.view.bounds];
//	CGRect rect = [tableViewCell convertRect:deleteButton.frame toView:nil];
//	__weak typeof (self) wself = self;
//	[deleteView showDeleteViewFromPoint:rect.origin clickBlock:^{
////		[self.dataArray removeObjectsInArray:@[[self.tableView indexPathForCell:tableViewCell]]];
//		__strong typeof (self)strongself = wself;
//		
//		[strongself.dataArray removeLastObject];
//		[strongself.tableView deleteRowsAtIndexPaths:@[[strongself.tableView indexPathForCell:tableViewCell]] withRowAnimation:UITableViewRowAnimationAutomatic];
//	}];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 100;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	GTListItem *item = [self.dataArray objectAtIndex:indexPath.row];
	
//	__kindof UIViewController *controller = [GTMediator detailViewControllerWithUrl:item.url];
	//[[GTDetailViewController alloc]initWithUrlString:item.url];
	
	
//	[GTMediator openUrl:@"detail://" params:@{@"url":item.url, @"controller":self.navigationController, @"title": [NSString stringWithFormat:@"第%@项",@(indexPath.row)]}];
	
	Class cls = [GTMediator classForProtocol:@protocol(GTDetailViewControlProtocol)];
	__kindof UIViewController *controller  = [[cls alloc]detailViewControllerWithUrl:item.url];
	
	
	controller.title = [NSString stringWithFormat:@"第%@项",@(indexPath.row)];
	controller.view.backgroundColor=[UIColor whiteColor];
	[self.navigationController pushViewController:controller animated:YES];
	
	
	
	
	//存储id
	[[NSUserDefaults standardUserDefaults]setBool:YES forKey:item.uniquekey];
	
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
